package com.settlement.system.converter;

import com.alibaba.excel.converters.string.StringStringConverter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import org.apache.commons.lang3.StringUtils;

import java.util.regex.Pattern;

/**
 * @author author
 */
public class BigNumberTabFormatConvert extends StringStringConverter {

    @Override
    public WriteCellData convertToExcelData(String value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (globalConfiguration==null && !StringUtils.isEmpty(value)&&isNumber(value)) {
            return new WriteCellData(value + "\t");
        }
        return super.convertToExcelData(value, contentProperty, globalConfiguration);
    }

    @Override
    public String convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) {
        if (cellData.getType() == CellDataTypeEnum.NUMBER && cellData.getNumberValue() != null) {
            return cellData.getNumberValue().toString();
        }
        return cellData.getStringValue();
    }

    private static boolean isNumber(String content){
        return Pattern.matches("^\\d+$", content.trim());
    }

    public static void main(String[] args) {
        System.out.println(isNumber("nihao123"));
        System.out.println(isNumber("123"));
        System.out.println(isNumber("123nihao"));
    }
}
